<!--注册用户-->
<template>
  <div class="main_box">
    <div class="forget_panel">
      <el-steps :active="step" class="order_step_box" align-center>
        <el-step title="验证身份"></el-step>
        <el-step title="设置密码"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- 第一步 -->
      <el-form
        :model="forgetForm"
        label-width="0px"
        v-if="step == 1"
        class="forget_form"
      >
        <!-- <el-radio
          v-model="type"
          label="1"
          @change="forgetForm.mobile = '',forgetForm.verificationCode = ''"
        >个人用户</el-radio>
        <el-radio
          v-model="type"
          label="2"
          @change="forgetForm.mobile = '',forgetForm.verificationCode = ''"
        >企业用户</el-radio> -->
        <el-form-item>
          <el-input
            v-model="forgetForm.mobile"
            placeholder="请输入手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item class="inline_item">
          <el-input
            v-model="forgetForm.verificationCode"
            style="width: 265px"
            placeholder="请输入短信验证码"
          ></el-input>
          <div class="get_code" @click="getSmsByResetPass">{{ waitMsg }}</div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="next_btn" @click="next(2)"
            >下一步</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 第二步 -->
      <div class="set_pwd" v-if="step == 2">
        <p class="info">
          当前手机号：
          <span>{{
            forgetForm.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
          }}</span>
        </p>
        <el-form :model="setPwdForm" label-width="0px">
          <el-form-item>
            <el-input
              v-model="setPwdForm.pwd"
              placeholder="请输入新密码"
              type="password"
              autocomplete="new-password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="setPwdForm.surePwd"
              placeholder="确认新密码"
              type="password"
              autocomplete="new-password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="next_btn" @click="next(3)"
              >下一步</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <!-- 第三步 -->
      <div class="set_finish" v-if="step == 3">
        <span class="iconfont iconwancheng1"></span>
        <p>
          请牢记您设置的新密码，
          <span class="num">{{ loginWait }}</span
          >秒后返回
          <span class="to_login" @click="loginPage">登录页</span>
        </p>
      </div>
    </div>
  </div>
</template>
<script>
import utils from "@/utils/index.js";

export default {
  layout: 'userLay',
  data () {
    return {
      type: "1",
      forgetForm: {
        mobile: "",
        verificationCode: ""
      },
      setPwdForm: {
        pwd: "",
        surePwd: ""
      },
      waitMsg: "获取验证码",
      wait: 60,
      loginWait: 5,
      step: 1 //当前步骤
    }
  },
  methods: {
    //获取短信验证码
    getSmsByResetPass () {
      let mobile = this.forgetForm.mobile || "";
      if (!mobile.trim()) {
        this.$message({
          message: "请输入手机号码",
          type: "warning"
        });
        return false;
      }
      if (!utils.verifyPhone(mobile)) {
        this.$message({
          message: "手机号码格式不正确",
          type: "warning"
        });
        return false;
      }
      if (this.wait == 60) {
        api
          .getSmsByResetPass({
            mobile: this.forgetForm.mobile
          })
          .then(res => {
            if (res.data.code == 200) {
              this.timer();
              this.$message({
                message: "短信验证码已发送",
                type: "success"
              });
            }
          });
      }
    },
    // 秒后重新发送
    timer () {
      if (this.wait == 0) {
        this.wait = 60;
        this.waitMsg = "获取验证码";
      } else {
        this.waitMsg = this.wait + "秒后重新发送";
        this.wait--;
        setTimeout(() => {
          this.timer();
        }, 1000);
      }
    },
    loginPage () {
      this.$router.push({
        name: "login"
      });
    },
    countDown () {
      if (this.loginWait == 1) {
        this.loginPage();
      } else {
        this.loginWait--;
        setTimeout(() => {
          this.countDown();
        }, 1000);
      }
    },
    next (index) {
      if (index == 2) {
        //校验短信验证码
        let mobile = this.forgetForm.mobile || "";
        if (!mobile.trim()) {
          this.$message({
            message: "请输入手机号码",
            type: "warning"
          });
          return false;
        }
        if (!utils.verifyPhone(mobile)) {
          this.$message({
            message: "手机号码格式不正确",
            type: "warning"
          });
          return false;
        }
        if (!this.forgetForm.verificationCode) {
          this.$message({
            message: "请输入短信验证码",
            type: "warning"
          });
          return false;
        }
        if (!/^[0-9]{6}$/.test(this.forgetForm.verificationCode)) {
          this.$message({
            message: "短信验证码格式不对",
            type: "warning"
          });
          return false;
        }
        api
          .checkSmsByResetPass({
            mobile: this.forgetForm.mobile,
            verificationCode: this.forgetForm.verificationCode
          })
          .then(res => {
            if (res.data.code == 200) {
              this.step = index;
            }
          });
      } else if (index == 3) {
        //重置密码
        if (utils.verifyPwd(this.setPwdForm.pwd)) {
          this.$message({
            message:
              "密码中至少包含大写字母、小写字母、数字和特殊字符中的3种，且至少6个字符",
            type: "warning"
          });
          return false;
        }
        if (this.setPwdForm.pwd != this.setPwdForm.surePwd) {
          this.$message({
            message: "密码不一致",
            type: "warning"
          });
          return false;
        }
        let passwordSafeScale = "";
        if (
          this.setPwdForm.pwd.length >= 6 &&
          this.setPwdForm.pwd.length <= 11
        ) {
          passwordSafeScale = "0.5";
        }
        if (
          this.setPwdForm.pwd.length >= 12 &&
          this.setPwdForm.pwd.length <= 16
        ) {
          passwordSafeScale = "1";
        }
        api
          .memberResetPass({
            mobile: this.forgetForm.mobile,
            verificationCode: this.forgetForm.verificationCode,
            password: utils.md532(this.setPwdForm.pwd),
            passwordSafeScale: passwordSafeScale
          })
          .then(res => {
            if (res.data.code == 200) {
              this.step = index;
              this.loginWait = 5;
              utils.clearCookie("userToken");
              this.$store.commit("SET_MEMBERID", 0);
              this.$store.commit("SET_UPDATECARNUM", 0);
              this.$store.commit("SET_USERINFO", null);
              api.initHeader();
              //倒计时5s
              this.countDown();
            }
          });
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.main_box {
  border-top: 1px solid #f0efef;
  background: #fff;
  .forget_panel {
    padding-bottom: 210px;
    padding-top: 52px;
    width: 521px;
    margin-left: auto;
    margin-right: auto;
  }
  .next_btn {
    width: 100%;
    margin-top: 10px;
  }
  .forget_form {
    padding-top: 32px;
    width: 384px;
    margin-left: auto;
    margin-right: auto;
    .el-radio {
      margin: 0 0 33px 12px;
    }
  }
  .set_pwd {
    padding-top: 32px;
    width: 377px;
    margin-left: auto;
    margin-right: auto;
    .info {
      font-size: 12px;
      color: #8e8e8e;
      text-align: center;
      padding-bottom: 14px;
      > span {
        color: #424242;
      }
    }
  }
  .set_finish {
    text-align: center;
    padding-top: 64px;
    .iconfont {
      color: #3ebd19;
      font-size: 24px;
      display: inline-block;
      vertical-align: middle;
    }
    > p {
      display: inline-block;
      vertical-align: middle;
      font-size: 14px;
      color: #424242;
      padding-left: 12px;
      .num {
        color: #fb363f;
        font-size: 16px;
      }
      .to_login {
        color: #2b8dff;
        cursor: pointer;
        padding-left: 4px;
      }
    }
  }
  .get_code {
    width: 100px;
    height: 40px;
    display: inline-block;
    vertical-align: bottom;
    cursor: pointer;
    margin-left: 12px;
    background: rgba(230, 229, 229, 1);
    border: 1px solid rgba(224, 223, 223, 1);
    color: #5b5b5b;
    font-size: 12px;
    line-height: 40px;
    text-align: center;
  }
}
</style>